/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import com.rh.game.Character;

import com.rh.game.chat.ChatImage;

import com.rh.game.impl.Character1;

import com.rh.util.Parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Hayden
 */
public class CharacterBuffer extends AbstractXmlBuffer {
    private static void debug(Object o)  {
        // System.out.println("CharacterBuffer: " + o);
    }
    
    public static enum Mode  {
        IMAGES, LOCATION;
    }
    
    /*
            <ID>Ned</ID>
            <Name>Ned</Name>
            <Gender>MALE</Gender>
            <location>
                <ZoneID>NULL</ZoneID>
                <X>0</X>
                <Y>0</Y>
            </location>
            <ChatImage>/resources/images/nerd.jpg</ChatImage>
            <TileID>10</TileID>
            public Character1(String pID, String pName, Location pLoc, Gender pGender, ChatImage pImage, byte pGridID)  {
     */
    private String id = "";
    private String name = "";
    private LocationBuffer locBuffer = new LocationBuffer();
    private String gender = "";
    private String tileID = "";
    private ShowImageBuffer imageBuffer = new ShowImageBuffer();
    
    private void init()  {
        id = "";
        name = "";
        locBuffer = new LocationBuffer();
        gender = "";
        tileID = "";
        imageBuffer = new ShowImageBuffer();
    }
    
    private Mode mode = null;

    private List<Character> npcs = new ArrayList<Character>();
    public List<Character> getNpcs()  { return npcs; }
    
    void characters(String pValue) {
        pValue = Parser.trim(pValue);
        if (pValue.length() == 0)  return;
        
        if (mode == Mode.LOCATION)  {
            locBuffer.characters(pValue);
        }
        else if (mode == Mode.IMAGES)  {
            imageBuffer.characters(pValue);
        }
        else if ("ID".equals(getStartElement()))  {
            id = pValue;
        }
        else if ("Name".equals(getStartElement()))  {
            name = pValue;
        }
        else if ("Gender".equals(getStartElement()))  {
            gender = pValue;
        }
        else if ("TileID".equals(getStartElement()))  {
            tileID = pValue;
        }
    }
    
    void startElement(String pName)  {
        super.startElement(pName);
        if ("location".equals(pName))  {
            mode = Mode.LOCATION;
        }
        else if ("images".equals(pName))  {
            mode = Mode.IMAGES;
        }
        
        if (mode == Mode.LOCATION)  {
            locBuffer.startElement(pName);
        }
        else if (mode == Mode.IMAGES)  {
            imageBuffer.startElement(pName);
        }
    }
    
    void endElement(String pName)  {
        if (mode == Mode.IMAGES)  {
            imageBuffer.endElement(pName);
        }
        
        if (("npc".equals(pName)) || ("player".equals(pName))) {
            // Add NPC to List
            String defaultImage = imageBuffer.getDefaultImage();
            byte tileByte = Byte.valueOf(tileID).byteValue();
            
            Character1 c1 = new Character1(id, name, locBuffer.getLocation(), Character.Gender.valueOf(gender), new ChatImage(defaultImage), tileByte);
            debug("Add NPC: " + id + ", " + name + ", " + locBuffer.getLocation() + ", " + gender);
            npcs.add(c1);
            // System.out.println("NPCS: " + npcs);
            for (Map.Entry<String, List<String>> entry : imageBuffer.getSpecial().entrySet())  {
               c1.addSpecialImage(entry.getKey(), entry.getValue()); 
            }
            // 
            // Now set the images
        }
        else if ("images".equals(pName))  {
            mode = null;
        }
        else if ("location".equals(pName))  {
            mode = null;
        }
    }    
}
